//
//  UrlHelper.m
//  FengHuangFM
//
//  Created by lanou3g on 15/12/15.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "UrlHelper.h"
#import "ScrollImageModel.h"
#import "ListCategoryModel.h"
#import "CategoryItemModel.h"
#import <AFNetworkReachabilityManager.h>
@implementation UrlHelper
/*---------------------------------------
 主页数据模型请求方法
 --------------------------------------- */
- (void)getDictionaryWithUrl:(NSString *)urlStr urlBody:(NSString *)body success:(void(^)(id))success fail:(void(^)(NSError *))fail
{
    //判断url是否为空
    if (!urlStr) {
        return;
    }
    //请求数据
   NSMutableArray *array = [NSMutableArray array];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"post"];
    NSData *data = [body dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data == nil || error != nil) {
            return ;
        }
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *dic in dataDic[@"data"]) {
            ListCategoryModel *caModel = [[ListCategoryModel alloc] init];
            caModel.ReourceArray = [NSMutableArray array];
            caModel.cardTitle = dic[@"cardTitle"];
            if ([dic[@"cardTitle"] isEqualToString:@"专题"]) {
                caModel.themeArray = [NSMutableArray array];
                for (NSDictionary *dict in dic[@"listCategory"][@"listSpecial"]) {
                    ThemeModel *themeModel = [[ThemeModel alloc] init];
                    [themeModel setValuesForKeysWithDictionary:dict];
                    [caModel.themeArray addObject:themeModel];
                }
            } else {
                if (dic[@"listCategory"] == nil) {
                    for (NSDictionary *sonDic in dic[@"listReource"]) {
                        [self dictionaryAnalysis:sonDic model:caModel];
                    }
                } else {
                    for (NSDictionary *sonDic in dic[@"listCategory"][@"listReource"]) {
                        [self dictionaryAnalysis:sonDic model:caModel];
                    }
                }
            }
            if ([dic[@"cardTitle"] isEqualToString:@"直播台"]) {
            } else {
                [array addObject:caModel];
            }
        }
        success(array);
    }];
    [task resume];
}

- (void)dictionaryAnalysis:(NSDictionary *)dic model:(ListCategoryModel *)caModel{
    ListReourceModel *reModel = [[ListReourceModel alloc] init];
    [reModel setValuesForKeysWithDictionary:dic];
     //NSLog(@"%@",reModel.img100_100);
    reModel.audioArray = [NSMutableArray array];
    for (NSDictionary *childDic in dic[@"audiolist"]) {
        [reModel.audioArray addObject:childDic[@"filePath"]];
    }
    if (caModel.ReourceArray) {
        [caModel.ReourceArray addObject:reModel];
    } else {
        caModel.ReourceArray = [NSMutableArray array];
        [caModel.ReourceArray addObject:reModel];
    }
}

/*---------------------------------------
 轮播图
 --------------------------------------- */
- (void)getScrollImageWithUrl:(NSString *)urlStr urlBody:(NSString *)body success:(void(^)(NSArray *))success fail:(void(^)(NSError *))fail {
    //判断url是否为空
    if (!urlStr) {
        return;
}
    //请求数据
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"post"];
    NSData *data = [body dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    NSURLSession *session = [NSURLSession sharedSession];
    NSMutableArray *array = [NSMutableArray array];
    NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data == nil || error != nil) {
            return ;
        }
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *dict in dic[@"data"]) {
            ScrollImageModel *model = [[ScrollImageModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [array addObject:model.img640_292];
        }
        success(array);
    }];
    [task resume];
}

/*---------------------------------------
 分类详情模型
 --------------------------------------- */
- (void)getCategoryItemModelWithUrl:(NSString *)urlStr urlBody:(NSString *)body success:(void(^)(NSArray *))success fail:(void(^)(NSError *))fail {
    //判断url是否为空
    if (!urlStr) {
        return;
    }
    //请求数据
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"post"];
    NSData *data = [body dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    NSURLSession *session = [NSURLSession sharedSession];
    NSMutableArray *array = [NSMutableArray array];
    NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data == nil || error != nil) {
            return ;
        }
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        if (dic[@"data"][@"hotList"]) {
            [self categoryModelWithDic:dic withStr:@"hotList" array:array];
        } else if (dic[@"data"][@"reList"]) {
            [self categoryModelWithDic:dic withStr:@"reList" array:array];
        } else if (dic[@"data"][@"newList"]) {
            [self categoryModelWithDic:dic withStr:@"newList" array:array];
        } else {
            return;
        }
        success(array);
    }];
    [task resume];
}

- (void)categoryModelWithDic:(NSDictionary *)dic withStr:(NSString *)str array:(NSMutableArray *)array {
    for (NSDictionary *dict in dic[@"data"][str]) {
        CategoryItemModel *model = [[CategoryItemModel alloc] init];
        [model setValuesForKeysWithDictionary:dict];
        NSLog(@"%@",model.compere);
        [array addObject:model];
    }
}

@end
